{% extends 'myadmin/base.html' %}

{% block title %}
<title>Book-Shop-图书分类列表</title>
{% endblock %}

{% block con %}
<div class="container-fluid am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-9">
            <div class="page-header-heading"><span class="am-icon-home page-header-heading-icon"></span> 图书分类管理
            </div>
            <div id="alert" class="am-alert am-alert-success" data-am-alert style="white-space:nowrap; position: absolute;top:-50px;left:200px;display: none;">
                <button type="button" class="am-close">&times;</button>
                <p></p>
            </div>
        </div>
    </div>
</div>

<div class="row-content am-cf">
    <div class="row  am-cf">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">分类列表</div>
                </div>
                <div class="widget-body  am-fr">

                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <a href="{% url 'myadmin_types_add' %}"
                                       class="am-btn am-btn-default am-btn-success"><span
                                            class="am-icon-plus"></span> 新增
                                    </a>
                                    <button type="button" class="am-btn am-btn-default am-btn-secondary"><span
                                            class="am-icon-save"></span> 保存
                                    </button>
                                    <button type="button" class="am-btn am-btn-default am-btn-warning"><span
                                            class="am-icon-archive"></span> 审核
                                    </button>
                                    <button type="button" class="am-btn am-btn-default am-btn-danger"><span
                                            class="am-icon-trash-o"></span> 删除
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                        <div class="am-form-group tpl-table-list-select">
                            <select data-am-selected="{btnSize: 'sm'}" style="display: none;">
                                <option value="option1">所有类别</option>
                                <option value="option2">IT业界</option>
                                <option value="option3">数码产品</option>
                                <option value="option3">笔记本电脑</option>
                                <option value="option3">平板电脑</option>
                                <option value="option3">只能手机</option>
                                <option value="option3">超极本</option>
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                        <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                            <input type="text" class="am-form-field ">
                            <span class="am-input-group-btn">
                                            <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"
                                                    type="button"></button>
                                          </span>
                        </div>
                    </div>

                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                            <tr>
                                <th>id</th>
                                <th>分类名</th>
                                <th>所属父类</th>
                                <th>路径</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for i in data %}
                            <tr class="gradeX">
                                <td class="am-text-middle">{{ i.id }}</td>
                                <td class="am-text-middle">{{ i.sb }}<span class="tname" isclick="0">{{ i.name }}</span></td>
                                <td class="am-text-middle">{% if i.pid == 0 %}顶级分类{% else %}{{ i.pname }}{% endif %}
                                </td>
                                <td class="am-text-middle">{{ i.path }}</td>
                                <td class="am-text-middle">
                                    <div class="tpl-table-black-operation">
                                        <a href="javascript:;" class="tpl-table-black-operation-del">
                                            <i class="am-icon-trash"></i> 删除
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                            <!-- more data -->
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">

                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">
                                <li class="am-disabled"><a href="#">«</a></li>
                                <li class="am-active"><a href="#">1</a></li>
                                <li><a href="#">2</a></li>
                                <li><a href="#">3</a></li>
                                <li><a href="#">4</a></li>
                                <li><a href="#">5</a></li>
                                <li><a href="#">»</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script>
    // 删除弹窗的显示与隐藏
    function showmsg(msg,type=true) {
        // console.log(msg,type)
        if (type) {
            $('#alert').addClass('am-alert am-alert-success')
        }else{
            $('#alert').addClass('am-alert am-alert-danger')
        }
        // 赋值一个文本
        $('#alert').find('p').text(msg)
        // 显示出来
        $('#alert').show()
        setTimeout(function () {
            $('#alert').hide()
        }, 3000)
    }

    // 封装编辑函数
    function typenameedit($this){
        $this.attr('isclick','1')

        // 先获取name
        tname = $this.text()
        // 创建一个input
        inp = $('<input type="text" value="'+tname+'" style="color: black;">')
        // 把input加入到span中
        $this.html(inp)
        // 使元素是选中状态
        inp.select()

        // 给input绑定丧失焦点事件,获取新的类名
        inp.blur(function () {
            newname = $(this).val()
            // 判断newname是否更新了
            if(newname == tname){
                // 不需要后台数据更新
                $this.text(tname)
            }else{
                // 发送ajax请求后后台修改数据
                id = $this.parents('tr').find('td:first').text()
                $.get('{% url "myadmin_types_edit" %}',{'id':id,'name':newname},function (data) {
                    if(data['code'] == 0){
                        // 成功
                        $this.text(newname)
                        showmsg(data['msg'],true)
                    }else{
                        // 失败
                        $this.text(tname)
                        showmsg(data['msg'],false)
                    }
                },'json')
            }
            $this.attr('isclick','0')
        })
    }

    // ajax修改:绑定双击事件
    $('.tname').dblclick(function () {
        is = $(this).attr('isclick')
        // console.log(is,typeof(is))
        if(is == '0'){
            typenameedit($(this))
        }

    })


    // ajax删除:绑定单击事件
    $('.tpl-table-black-operation-del').click(function () {
        // 获取当前的数据id this是a链接
        id = $(this).parents('tr').find('td:first').text()
        a = $(this)
        // alert(id)
        // 发送ajax请求到后台 进行数据的删除
        // 大括号里面是模板引擎语法,先解析成字符串返回到浏览器中,才执行外面的,可以是单引号
        $.get('{% url "myadmin_types_delete" %}', {'id': id}, function (data) {
            // 判断是否成功
            if (data['code'] == 0) {
                // 删除当前的这一行数据 this是XMLHttpRequest,ajax对象
                // $(this).parents('tr').remove()
                // console.log($(this))
                a.parents('tr').remove()
                showmsg(data['msg'],true)
            }else{
                showmsg(data['msg'],false)
            }
            // alert(data['msg'])
        }, 'json')
    })
</script>
{% endblock %}

